import Vue from 'vue';
import Router from 'vue-router';

Vue.use(Router);

const router = new Router({
    routes: [
        {
            path: '/',
            redirect: '/admin'
        },
        {
            path: '/login',
            component: resolve => require(['page/admin/Login.vue'], resolve)
        },
        {
            path: '/admin',
            component: resolve => require(['../components/common/Home.vue'], resolve),
            children:[
                {
                    path: '/',
                    component: resolve => require(['../components/page/Readme.vue'], resolve)
                },
                {
                    path: '/index',
                    component: resolve => require(['../components/page/Readme.vue'], resolve)
                },
                {
                    path:'/project',
                    component: resolve => require(['page/admin/project/Index.vue'], resolve)
                },
                {
                    path:'/application',
                    component: resolve => require(['page/admin/projectApplication/Index.vue'], resolve)
                },
                {
                    path:'/projectUpdate',
                    component: resolve => require(['page/admin/update/Index.vue'], resolve)
                },
                {
                    path:'/conclude',
                    component: resolve => require(['page/admin/concludeApplication/Index.vue'], resolve)
                },
                {
                    path: '/user',
                    component: resolve => require(['page/admin/user/Index.vue'], resolve)
                },
                {
                    path: '/role',
                    component: resolve => require(['page/admin/role/Index.vue'], resolve)
                },
                {
                    path: '/permission',
                    component: resolve => require(['page/admin/permission/Index.vue'], resolve)
                },
                {
                    path: '/dataDictionary',
                    component: resolve => require(['page/admin/dict/Index.vue'], resolve)
                },
                {
                    path: '/organization',
                    component: resolve => require(['page/admin/organization/Index.vue'], resolve)
                },
                {
                    path: '/notice',
                    component: resolve => require(['page/admin/notice/Index.vue'], resolve)
                }
            ]
        }
    ]
})

//路由前先进行判断是否存在token，没有token的会先跳转到登录页面
router.beforeEach((to,from,next)=>{
    if (to.path === "/login"){
        next();
    }else {
        let token  = localStorage.getItem("token");
        if (token ==null || token == ""){
            console.log("token为空，即将跳转登录页面");
            next("/login");
        }else {
            next();
        }
    }

})
export default router
